package com.ptbuy.frebook;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.ptbuy.MainActivity;
import com.ptbuy.R;
import com.ptbuy.utils.Constants;
import com.ptbuy.utils.ToastUtil;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class FrebootActivity extends AppCompatActivity {


    private String addFeedback = Constants.API + File.separator + "feedback" + File.separator + "addFeedback/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frebook);
        EditText book = findViewById(R.id.book);
        Button submitBtn = findViewById(R.id.submitBtn);
        //返回
        findViewById(R.id.back).setOnClickListener(v -> {
            back();
        });

        submitBtn.setOnClickListener(v -> {
            String s = book.getText().toString();
            if (s != null && !"".equalsIgnoreCase(s)) {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient.Builder()
                            .connectTimeout(15, TimeUnit.SECONDS)
                            .readTimeout(10, TimeUnit.HOURS)
                            .writeTimeout(10, TimeUnit.HOURS)
                            .build();

                    Request.Builder builder = new Request.Builder()
                            .url(addFeedback + "?content=" + s)
                            .get();
                    Request request = builder.build();
                    okHttpClient.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(@NotNull Call call, @NotNull IOException e) {
                            runOnUiThread(()->{
                                ToastUtil.show(FrebootActivity.this, "网络请求失败");
                            });

                        }

                        @Override
                        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                            runOnUiThread(()-> {
                                ToastUtil.show(FrebootActivity.this, "感谢您的宝贵建议");
                            });
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                ToastUtil.show(this, "请输入内容");
            }
        });

    }

    private void back() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            back();
        }
        return false;
    }
}